from flask import Blueprint, render_template, request
from app.models import User
from app import db

register_page = Blueprint('register_page', __name__, template_folder='templates')


@register_page.route('/register', methods=['GET', 'POST'])
def register():
    data = {}
    if request.method == 'POST':
        hs_username = request.values.get('username')
        hs_email = request.values.get('emailaddress')
        hs = User()
        hs.set_password = request.values.get('password')
        hs_obj = User(username=hs_username, email_address=hs_email, password_hash=hs.password_hash)
        try:
            db.session.add(hs_obj)
        except:
            data["msg"] = "<script>alert('注册失败！')</script>"
        else:
            db.session.commit()
            data["msg"] = "<script>alert('注册成功！')</script>"

    return render_template('hs-register.html', jsondata=data)
